package api

import (
	"net/http"

	"example.com/rest/example/dao"
	"example.com/rest/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configInvoiceItemsRouter(router *httprouter.Router) {
	router.GET("/invoiceitems", GetAllInvoiceItems)
	router.POST("/invoiceitems", AddInvoiceItems)
	router.GET("/invoiceitems/:argInvoiceLineID", GetInvoiceItems)
	router.PUT("/invoiceitems/:argInvoiceLineID", UpdateInvoiceItems)
	router.DELETE("/invoiceitems/:argInvoiceLineID", DeleteInvoiceItems)
}

func configGinInvoiceItemsRouter(router gin.IRoutes) {
	router.GET("/invoiceitems", ConverHttprouterToGin(GetAllInvoiceItems))
	router.POST("/invoiceitems", ConverHttprouterToGin(AddInvoiceItems))
	router.GET("/invoiceitems/:argInvoiceLineID", ConverHttprouterToGin(GetInvoiceItems))
	router.PUT("/invoiceitems/:argInvoiceLineID", ConverHttprouterToGin(UpdateInvoiceItems))
	router.DELETE("/invoiceitems/:argInvoiceLineID", ConverHttprouterToGin(DeleteInvoiceItems))
}

// GetAllInvoiceItems is a function to get a slice of record(s) from invoice_items table in the main database
// @Summary Get list of InvoiceItems
// @Tags InvoiceItems
// @Description GetAllInvoiceItems is a handler to get a slice of record(s) from invoice_items table in the main database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.InvoiceItems}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /invoiceitems [get]
// http "http://localhost:8080/invoiceitems?page=0&pagesize=20" X-Api-User:user123
func GetAllInvoiceItems(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "invoice_items", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllInvoiceItems(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetInvoiceItems is a function to get a single record from the invoice_items table in the main database
// @Summary Get record from table InvoiceItems by  argInvoiceLineID
// @Tags InvoiceItems
// @ID argInvoiceLineID
// @Description GetInvoiceItems is a function to get a single record from the invoice_items table in the main database
// @Accept  json
// @Produce  json
// @Param  argInvoiceLineID path int true "InvoiceLineId"
// @Success 200 {object} model.InvoiceItems
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /invoiceitems/{argInvoiceLineID} [get]
// http "http://localhost:8080/invoiceitems/1" X-Api-User:user123
func GetInvoiceItems(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argInvoiceLineID, err := parseInt32(ps, "argInvoiceLineID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "invoice_items", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetInvoiceItems(ctx, argInvoiceLineID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddInvoiceItems add to add a single record to invoice_items table in the main database
// @Summary Add an record to invoice_items table
// @Description add to add a single record to invoice_items table in the main database
// @Tags InvoiceItems
// @Accept  json
// @Produce  json
// @Param InvoiceItems body model.InvoiceItems true "Add InvoiceItems"
// @Success 200 {object} model.InvoiceItems
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /invoiceitems [post]
// echo '{"invoice_line_id": 30,"invoice_id": 80,"track_id": 11,"unit_price": 0.13639347552478345,"quantity": 56}' | http POST "http://localhost:8080/invoiceitems" X-Api-User:user123
func AddInvoiceItems(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	invoiceitems := &model.InvoiceItems{}

	if err := readJSON(r, invoiceitems); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := invoiceitems.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	invoiceitems.Prepare()

	if err := invoiceitems.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "invoice_items", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	invoiceitems, _, err = dao.AddInvoiceItems(ctx, invoiceitems)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, invoiceitems)
}

// UpdateInvoiceItems Update a single record from invoice_items table in the main database
// @Summary Update an record in table invoice_items
// @Description Update a single record from invoice_items table in the main database
// @Tags InvoiceItems
// @Accept  json
// @Produce  json
// @Param  argInvoiceLineID path int true "InvoiceLineId"
// @Param  InvoiceItems body model.InvoiceItems true "Update InvoiceItems record"
// @Success 200 {object} model.InvoiceItems
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /invoiceitems/{argInvoiceLineID} [put]
// echo '{"invoice_line_id": 30,"invoice_id": 80,"track_id": 11,"unit_price": 0.13639347552478345,"quantity": 56}' | http PUT "http://localhost:8080/invoiceitems/1"  X-Api-User:user123
func UpdateInvoiceItems(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argInvoiceLineID, err := parseInt32(ps, "argInvoiceLineID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	invoiceitems := &model.InvoiceItems{}
	if err := readJSON(r, invoiceitems); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := invoiceitems.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	invoiceitems.Prepare()

	if err := invoiceitems.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "invoice_items", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	invoiceitems, _, err = dao.UpdateInvoiceItems(ctx,
		argInvoiceLineID,
		invoiceitems)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, invoiceitems)
}

// DeleteInvoiceItems Delete a single record from invoice_items table in the main database
// @Summary Delete a record from invoice_items
// @Description Delete a single record from invoice_items table in the main database
// @Tags InvoiceItems
// @Accept  json
// @Produce  json
// @Param  argInvoiceLineID path int true "InvoiceLineId"
// @Success 204 {object} model.InvoiceItems
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /invoiceitems/{argInvoiceLineID} [delete]
// http DELETE "http://localhost:8080/invoiceitems/1" X-Api-User:user123
func DeleteInvoiceItems(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argInvoiceLineID, err := parseInt32(ps, "argInvoiceLineID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "invoice_items", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeleteInvoiceItems(ctx, argInvoiceLineID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
